/**
 * Copyright © 2011 Creativor Studio.
 * About license information, please see LICENSE.txt.
 */
package org.creativor.rayson.demo.main;

import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.creativor.rayson.api.CallFuture;
import org.creativor.rayson.api.ServiceRegistration;
import org.creativor.rayson.api.TransferSocket;
import org.creativor.rayson.client.Rayson;
import org.creativor.rayson.demo.protocol.DemoProtocol;
import org.creativor.rayson.demo.proxy.DemoAsyncProtocol;
import org.creativor.rayson.demo.proxy.DemoTransferArgument;
import org.creativor.rayson.exception.CallExecutionException;
import org.creativor.rayson.exception.IllegalServiceException;
import org.creativor.rayson.exception.NetWorkException;
import org.creativor.rayson.exception.ReadInvocationException;
import org.creativor.rayson.exception.RpcException;
import org.creativor.rayson.exception.ServiceNotFoundException;
import org.creativor.rayson.exception.UnsupportedVersionException;
import org.creativor.rayson.protocol.ServerProtocol;
import org.creativor.rayson.transport.server.ServerConfig;

/**
 * 
 * @author Nick Zhang
 */
public class Demo
{
	
	public static void main(String[] args) throws IllegalServiceException, NetWorkException,
			RpcException, IOException, ServiceNotFoundException, InterruptedException,
			UnsupportedVersionException, CallExecutionException
	{
		
		InetSocketAddress serverAddress =
				new InetSocketAddress(InetAddress.getLocalHost(), ServerConfig.DEFAULT_PORT_NUMBER);
		
		Rayson.ping(serverAddress);
		
		System.out.println("Ping sucessfully");
		
		ServerProtocol serverService = Rayson.getServerProxy(serverAddress);
		DemoProtocol testRpcProxy = Rayson.createProxy("demo", DemoProtocol.class, serverAddress);
		
		try
		{ // list services.
		
			for (ServiceRegistration registration : serverService.list())
			{
				System.out.println(registration.toString());
			}
			// testRpcService.voidMethod();
			
			// System.out.println(testRpcService.getIntArray());
			
			// int returnI = Rayson.call(2);
			// testRpcService.voidMethod();
			System.out.println(testRpcProxy.echo("hello world"));
			testRpcProxy.getInt();
			
		} catch (NullPointerException e)
		{
			// TODO: handle exception
			e.printStackTrace();
			
		} catch (RpcException e)
		{
			try
			{
				e.throwCause();
			} catch (UndeclaredThrowableException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (NetWorkException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ServiceNotFoundException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ReadInvocationException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnsupportedVersionException e1)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		// Test transfer socekt
		
		TransferSocket transferSocket =
				Rayson.openTransferSocket(serverAddress, new DemoTransferArgument("testPath"));
		
		transferSocket.getDataOutput().writeInt(1456);
		
		// close transfer socket.
		transferSocket.close();
		
		// test asynchronous call.
		DemoAsyncProtocol asyncProxy =
				Rayson.createAsyncProxy("demo", DemoAsyncProtocol.class, serverAddress);
		CallFuture<int[]> ddd = asyncProxy.getIntArray();
		int[] intarry = ddd.get();
		CallFuture<String> echoFuture;
		for (int i = 0; i < 3; i++)
		{
			try
			{
				echoFuture = asyncProxy.echo("Async call echo message");
				System.out.println("Async call echo:" + echoFuture.get());
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		long waitSeconds = 3;
		System.err.println("Sleep " + waitSeconds + " secondes.");
		Thread.sleep(waitSeconds * 1000);
		System.exit(0);
		
	}
}
